package com.wf.ew.orderB.biz.impl;

import com.wf.ew.common.BaseDao;
import com.wf.ew.common.PageResult;
import com.wf.ew.common.utils.Utils;
import com.wf.ew.system.model.User;
import com.wf.ew.common.constant.UserConstant;
import com.wf.ew.orderB.biz.CustomerBiz;
import com.wf.ew.orderB.model.Customer;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2020-04-13 13:56:30
 * @version ：1.0
 */
@Repository
public class CustomerBizImpl extends BaseDao implements CustomerBiz {

	@Override
	public String list(PageResult<T> page, Customer customer, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Customer> rm = new BeanPropertyRowMapper<Customer>(Customer.class);
		StringBuffer sql = new StringBuffer("select c.id,c.companyId,c.name");
        sql.append(",c.qrcode,c.reluserid,c.orderNum,c.createTime");
		sql.append(",c.createUser,c.updateTime,c.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from sszg_customer c ");
        sql.append(" inner join sys_user suser on suser.id=c.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=c.companyId ");
        sql.append(" where c.del=0 and cmy.del=0 ");
		if(!loginUser.getId().equals(UserConstant.ADMIN_ID)){
            sql.append(" and c.companyId=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId());
//			sql.append(" and c.createUser=:createUser ");
//			map.put("createUser", loginUser.getId());
        }
    	if(Utils.isNotEmpty(customer)){
			if(Utils.isNotEmpty(customer.getName())){
				sql.append(" and instr(c.name,:name)>0 ");
				map.put("name", customer.getName());
			}
			if(Utils.isNotEmpty(customer.getSort())&&Utils.isNotEmpty(customer.getOrder())){
				sql.append(" order by "+customer.getSort()+" "+customer.getOrder());
			}
		}
		return jsonPage(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<Customer> list(Customer customer, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Customer> rm = new BeanPropertyRowMapper<Customer>(Customer.class);
        StringBuffer sql = new StringBuffer("select c.id,c.companyId,c.name");
        sql.append(",c.qrcode,c.reluserid,c.orderNum,c.createTime");
		sql.append(",c.createUser,c.updateTime,c.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from sszg_customer c ");
        sql.append(" inner join sys_user suser on suser.id=c.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=c.companyId ");
        sql.append(" where c.del=0 and cmy.del=0 ");
		if(!loginUser.getId().equals(UserConstant.ADMIN_ID)){
            sql.append(" and c.companyId=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId());
//			sql.append(" and c.createUser=:createUser ");
//			map.put("createUser", loginUser.getId());
        }
    	if(Utils.isNotEmpty(customer)){
			if(Utils.isNotEmpty(customer.getName())){
				sql.append(" and instr(c.name,:name)>0 ");
				map.put("name", customer.getName());
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}
	@Override
	public List<Customer> customerListAll() {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Customer> rm = new BeanPropertyRowMapper<Customer>(Customer.class);
        StringBuffer sql = new StringBuffer("select c.id,c.companyId,c.name");
        sql.append(",c.qrcode,c.reluserid,c.orderNum,c.createTime");
		sql.append(",c.createUser,c.updateTime,c.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from sszg_customer c ");
        sql.append(" inner join sys_user suser on suser.id=c.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=c.companyId ");
        sql.append(" where c.del=0 and cmy.del=0 ");
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public Customer getById(Customer customer) {
		RowMapper<Customer> rm = new BeanPropertyRowMapper<Customer>(Customer.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select c.id,c.companyId,c.name");
        sql.append(",c.qrcode,c.reluserid,c.userNum,c.orderNum,c.createTime");
		sql.append(",c.createUser,c.updateTime,c.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from sszg_customer c ");
        sql.append(" inner join sys_user suser on suser.id=c.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=c.companyId ");
        sql.append(" where c.del=0 and cmy.del=0 and c.id=:id ");
		map.put("id", customer.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean add(Customer customer, User loginUser) {
		int count = 1;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into sszg_customer (");
        sql.append("companyId,name,qrcode,reluserid");
		sql.append(",orderNum,userNum,createUser) values ( ");
        sql.append(":companyId,:name,:qrcode,:reluserid");
		sql.append(",:userNum,:orderNum,:createUser)");
        map.put("companyId", loginUser.getCompanyId());
        map.put("name", customer.getName());
        map.put("qrcode", customer.getQrcode());
        map.put("reluserid", customer.getReluserid());
        map.put("userNum", customer.getUserNum()==null?null:customer.getUserNum()+"");
        map.put("orderNum", customer.getOrderNum()==null?null:customer.getOrderNum()+"");
		map.put("createUser", loginUser.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(Customer customer, User loginUser) {
		int count = 0;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update sszg_customer set ");
        sql.append("name=:name,qrcode=:qrcode,reluserid=:reluserid,userNum=:userNum ");
		sql.append(",orderNum=:orderNum,updateTime=now(),updateUser=:updateUser where id=:id ");
        map.put("name", customer.getName());
        map.put("qrcode", customer.getQrcode());
        map.put("reluserid", customer.getReluserid());
        map.put("userNum", customer.getUserNum()==null?null:customer.getUserNum()+"");
        map.put("orderNum", customer.getOrderNum()==null?null:customer.getOrderNum()+"");
		map.put("updateUser", loginUser.getId());
		map.put("id", customer.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean delete(Customer customer, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(customer.getIds().length()>0){
			for(String ID : customer.getIds().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update sszg_customer set del=1" +
						",updateUser=:updateUser, updateTime=now() " 
						+ " where id =:id ");
				map.put("updateUser", loginUser.getId());
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
